﻿using MediatR;
using T.Contract.API.Application.Command.TrackingCommand;
using T.Contract.Repository.Interface;

namespace T.Contract.API.Application.CommandHandlers.TrackingCommandHandler
{
    public class UpdateTrackingCommandHandler : IRequestHandler<UpdateTrackingCommand, int>
    {
        /// <summary>
        /// 仓储接口
        /// </summary>
        private readonly IContractRepository _contractRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contractRepository"></param>
        public UpdateTrackingCommandHandler(IContractRepository contractRepository)
        {
            _contractRepository = contractRepository;
        }

        /// <summary>
        /// 处理命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(UpdateTrackingCommand request, CancellationToken cancellationToken)
        {
            var res = await _contractRepository.GetModelAsync(request.ContractId);
            //发货
            if (request.Shipmentstatus == "运输中")
            {
                res.Shipmentstatus = request.Shipmentstatus;
                res.Abnormalalarms = "正常";
            }
            //签收
            else if (request.Shipmentstatus == "已完成")
            {
                res.Shipmentstatus = request.Shipmentstatus;
                res.Abnormalalarms = "-"; 
            }
            //回单
            else if (request.Shipmentstatus == "已回单")
            {
                res.Shipmentstatus = request.Shipmentstatus;
                res.Abnormalalarms = "-";
            }

            //修改状态
            return await _contractRepository.UpdateAsync(res);
        }
    }
}
